package megrez.mail.sender;

import java.util.Date;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import megrez.mail.sender.common.MailMessage;
import megrez.mail.sender.domain.MailContentType;
import megrez.mail.sender.domain.TemplateConfig;
import megrez.mail.sender.web.vo.TemplateConfigInput;

/**
 * 工具
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Component
public class Utils {
    @Autowired
    private Environment env;
    @Autowired
    private ObjectMapper objectMapper;    
    private static int counter = 0;

    public TemplateConfig createTemplateConfig() {
        return new TemplateConfig(MailContentType.TEXT, "c" + counter++, "r" + counter++, true);
    }

    public TemplateConfigInput createTemplateConfigInput() {
        return new TemplateConfigInput(MailContentType.TEXT, "c" + counter++, "r" + counter++, true);
    }

    public String asJsonString(Object o) {
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> T paseObject(String value, Class<T> clazz) {
        try {
            return objectMapper.readValue(value, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }
    
    public String getTestMailAddress() {
        return env.getProperty("mail.test_address");
    }

    public MailMessage createTextMailMessage() {
        MailMessage rslt = new MailMessage();
        rslt.setTemplateCode(CreateTemplateRunner.TEXT_TEMPLATE);
        rslt.addMail(getTestMailAddress());
        rslt.setLocale(Locale.getDefault());
        rslt.addParam("sendTime", new Date().toString());
        return rslt;
    }

    
    public MailMessage createThymeleafMailMessage() {
        MailMessage rslt = new MailMessage();
        rslt.setTemplateCode(CreateTemplateRunner.THYMELEAF_TEMPLATE);
        rslt.addMail(getTestMailAddress());
        rslt.setLocale(Locale.getDefault());
        rslt.addParam("sendTime", new Date().toString());
        return rslt;
    }

    public MailMessage createFreemarkerMailMessage() {
        MailMessage rslt = new MailMessage();
        rslt.setTemplateCode(CreateTemplateRunner.FREEMARKER_TEMPLATE);
        rslt.addMail(getTestMailAddress());
        rslt.setLocale(Locale.getDefault());
        rslt.addParam("sendTime", new Date().toString());
        return rslt;
    }

    public MailMessage createGroovyMailMessage() {
        MailMessage rslt = new MailMessage();
        rslt.setTemplateCode(CreateTemplateRunner.GROOVY_TEMPLATE);
        rslt.addMail(getTestMailAddress());
        rslt.setLocale(Locale.getDefault());
        rslt.addParam("sendTime", new Date().toString());
        return rslt;
    }

    public MailMessage createPug4jMailMessage() {
        MailMessage rslt = new MailMessage();
        rslt.setTemplateCode(CreateTemplateRunner.PUG4J_TEMPLATE);
        rslt.addMail(getTestMailAddress());
        rslt.setLocale(Locale.getDefault());
        rslt.addParam("sendTime", new Date().toString());
        return rslt;
    }
}
